package Objects;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Admin
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptUser {
    private String Hello = "siomaiho";
        public String Encrypt(String TBEncrypt) {
        try {        
                      
            byte[] byteData = Hello.getBytes("UTF8");
            SecretKeySpec secretKey = new SecretKeySpec(byteData, "DES");  
            Key mykey = secretKey;
// initialize the Cipher objects                       
            Cipher cipher = Cipher.getInstance("DES","SunJCE");
                        
            cipher.init(Cipher.ENCRYPT_MODE, mykey);
                        // creating the encrypting cipher stream           
            //String TBEncrypt = "YEAH";           
            byte[] conv = TBEncrypt.getBytes("UTF8");           
            byte[] raw = cipher.doFinal(conv);
            
            //  CipherInputStream cis = new CipherInputStream(fis, cipher);            
            BASE64Encoder encoder = new BASE64Encoder();	
            TBEncrypt = encoder.encode(raw);
            
           
        } 
        catch (Exception exp) {
      exp.printStackTrace();
    }return TBEncrypt;
        
  }
        
  public String decrypt(String encrypted)  
  {
      
    try{	// Get a cipher object.          
        byte[] byteData = Hello.getBytes("UTF8");
        SecretKeySpec secretKey = new SecretKeySpec(byteData, "DES");  
        Key mykey = secretKey;
	Cipher cipher = Cipher.getInstance("DES", "SunJCE");
	cipher.init(Cipher.DECRYPT_MODE, mykey);
 
	//decode the BASE64 coded message
	BASE64Decoder decoder = new BASE64Decoder();
	byte[] raw = decoder.decodeBuffer(encrypted);
 
	//decode the message
	byte[] stringBytes = cipher.doFinal(raw);
 
	//converts the decoded message to a String
	encrypted = new String(stringBytes, "UTF8");
	
    }
    catch(Exception exp){
        exp.printStackTrace();
        
    }
    return encrypted;
}

}
